package LobbyNode

import (
	"PBMessage"
	"Player"
	"time"

	"git.oschina.net/yangdao/SGNet2"
	"git.oschina.net/yangdao/extlib/log"

	"git.oschina.net/yangdao/goerl"
	"github.com/golang/protobuf/proto"
)

//LobbyNode 大厅节点
type LobbyNode struct {
	workNode *goerl.Node

	playerList  map[int64]*Player.GamePlayer
	tickAddTime float64
}

//New 创建大厅节点
func New() *LobbyNode {
	retNode := &LobbyNode{
		playerList: make(map[int64]*Player.GamePlayer),
	}
	retNode.workNode = goerl.NewNode(retNode.mailCenterHandler)
	retNode.workNode.UpdateFunc = retNode.update
	<-goerl.Register("LobbyNode", retNode.workNode)

	return retNode
}
func (lobbyNode *LobbyNode) getPlayer(deviceID string) *Player.GamePlayer {
	for _, player := range lobbyNode.playerList {
		if player.DevciveID == deviceID {
			return player
		}
	}
	return nil
}

//Start 启动大厅服务器
func (lobbyNode *LobbyNode) Start() {
	lobbyNode.workNode.Start()
}

func (lobbyNode *LobbyNode) mailCenterHandler(msg interface{}) {

	switch msg.(type) {
	case []interface{}:
		tuple := msg.([]interface{})
		msghead := tuple[0].(string)
		switch msghead {
		case ":ClientMail":
			lobbyNode.clientMailHandler(tuple[1].(uint16), tuple[2].(proto.Message), tuple[3].(*SGNet2.Session))
		case ":DBMail":
			lobbyNode.dbMailHandler(tuple)
		case ":SessionStateMail":
			sess := tuple[1].(*SGNet2.Session)
			msgType := tuple[2].(int)
			if msgType == SGNet2.SGNetMsg_SessionLost {
				log.Log("LobbyNode,Info", "退出大厅"+lobbyNode.playerList[sess.ID].DevciveID)
				delete(lobbyNode.playerList, sess.ID)
			}
		case ":GameMgrMail":
			msgType := tuple[1].(string)
			switch msgType {
			case ":res-add-new-player":
				player := lobbyNode.playerList[tuple[2].(int64)]
				player.State = Player.PlayerStateGame
				goerl.SendByName("NetNode", []interface{}{":CmdMail", ":player-state-change", player.SessID, player.State, tuple[3]})

				scMatch := &PBMessage.SC_StartMatch{}
				scMatch.Type = proto.Int32(0)
				goerl.SendByName("NetNode", []interface{}{":ToClientMail", player.SessID, int(PBMessage.PBID_SC_StartMatch), scMatch})
			}
		}
	case proto.Message:
	}
}

func (lobbyNode *LobbyNode) clientMailHandler(msgID uint16, msg proto.Message, sess *SGNet2.Session) {
	switch msgID {
	case PBMessage.PBID_CS_Login:
		lobbyNode.loginHandler(msg, sess)
	case PBMessage.PBID_CS_StartMatch:
		lobbyNode.matchHandler(msg, sess)
	}
}

func (lobbyNode *LobbyNode) dbMailHandler(tuple []interface{}) {
	msgHead := tuple[1].(string)
	switch msgHead {
	case ":res-register-login":
		lobbyNode.dbLoginHandler(tuple)
	}
}

func (lobbyNode *LobbyNode) update(dt int) {
	lobbyNode.tickAddTime += float64(dt / 1000.0)
	if lobbyNode.tickAddTime > 5 {
		lobbyNode.tickAddTime = 0
		lobbyNode.sendTick()
	}
}
func (lobbyNode *LobbyNode) sendTick() {
	scTick := &PBMessage.SC_LobbyTick{}
	scTick.Time = proto.Int64(time.Now().Unix())
	scTick.PlayerNumber = proto.Int64(int64(len(lobbyNode.playerList)))
	for _, player := range lobbyNode.playerList {
		if player.State == Player.PlayerStateLobby {
			goerl.SendByName("NetNode", []interface{}{":ToClientMail", player.SessID, int(PBMessage.PBID_SC_LobbyTick), scTick})
		}
	}
}
